let obj;
let query_task_name = '';
obj = JSON.parse(localStorage.getItem('userinfo'));
// console.log(obj);
$('.addTo').click(function() {
	$('.add_content').css('display', 'block');
})
$('.add_cancel').click(function() {
	$('.add_content').css('display', 'none');
})

function pullData() {
	$.ajax({
		url: 'http://chonghekj.com/todo/index.php/Home/Task/getList',
		type: 'get',
		dataType: 'json',
		data: {
			user_id: obj.user_id,
			access_token: obj.access_token,
			task_name: query_task_name,
		},
		success: function(res) {
			// console.log(res);
			// console.log(res.data[0].task_state)
			let html = '';
			let html2 = '';
			for (let i = 0; i < res.data.length; i++) {
				if (res.data[i].task_state == '0') {
					html +=
						`
							<div class="date">创建时间：${res.data[i].create_time}</div>
							<div class="event">
								<div class="event_left">
									<span class='title'>事件：</span><span class="event_name">${res.data[i].task_name}</span>
									<span class="renwu">事件<i class="dian"></i>描述</span>
									<span class="event_id">${res.data[i].task_id}</span>
									<span class="event_desc">${res.data[i].task_desc}</span>
									<span class="event_state">${res.data[i].task_state}</span>
								</div>
								<div class="event_right">
									<span class="edit"></span>
									<span class="complete"></span>
								</div>
							</div>
							`

				}else{
					html2 += `
							<div class="event">
								<div class="event_left">
									<span class='title'>事件：</span><span class="event_name">${res.data[i].task_name}</span>
									<span class="renwu">事件<i class="dian"></i>描述</span>
									<span class="event_id">${res.data[i].task_id}</span>
									<span class="event_desc">${res.data[i].task_desc}</span>
									<span class="event_state">${res.data[i].task_state}</span>
								</div>
								<div class="event_right">
									<span class="edit2"></span>
									<span class="complete2"></span>
								</div>
							</div>
							`
				}
				$('.subject_sub').empty().append(html);
				$('.total').html(res.data.length);
				$('.subject_sub2').html(html2);
				if (res.data.length > 0) {
					$('.blankTips').css('display', 'none');
				} else {
					$('.blankTips').css('display', 'block');
				}
			}
		}
	})
}
pullData();
$('.add_complete').click(function() {
	$.ajax({
		url: 'http://chonghekj.com/todo/index.php/Home/Task/add',
		type: 'post',
		dataType: 'json',
		data: {
			user_id: obj.user_id,
			access_token: obj.access_token,
			task_name: $('.add_item_name').val(),
			task_desc: $('.add_item_desc').val(),
		},
		success: function(res) {
			// console.log(res)
			pullData();
			$('.add_content').css('display', 'none');
			$('.add_item_name').val('');
			$('.add_item_desc').val('');
		}
	})
})

$('.subject_sub2').on('click', '.complete2', function() {
	task_id = $(this).parent().parent().find('.event_id').text();
	console.log(task_id);
	$.ajax({
		url: 'http://chonghekj.com/todo/index.php/Home/Task/delete',
		type: 'post',
		dataType: 'json',
		data: {
			user_id: obj.user_id,
			access_token: obj.access_token,
			task_id,
		},
		success: function(res) {
			// console.log(res);
			pullData();
		}
	})
})
$('.subject_sub').on('click', '.complete', function(){
	task_state = '1';
	task_id = $(this).parent().parent().find('.event_id').text();
	task_name = $(this).parent().parent().find('.event_name').text();
	task_desc = $(this).parent().parent().find('.event_desc').text();
	$.ajax({
		url: 'http://chonghekj.com/todo/index.php/Home/Task/edit',
		type: 'post',
		dataType: 'json',
		data: {
			user_id: obj.user_id,
			access_token: obj.access_token,
			task_id,
			task_name,
			task_desc,
			task_state
		},
		success: function(res) {
			// console.log(res);
			pullData();
		}
	})
})
$('.subject_sub2').on('click', '.edit2', function(){
	task_state = '0';
	task_id = $(this).parent().parent().find('.event_id').text();
	task_name = $(this).parent().parent().find('.event_name').text();
	task_desc = $(this).parent().parent().find('.event_desc').text();
	$.ajax({
		url: 'http://chonghekj.com/todo/index.php/Home/Task/edit',
		type: 'post',
		dataType: 'json',
		data: {
			user_id: obj.user_id,
			access_token: obj.access_token,
			task_id,
			task_name,
			task_desc,
			task_state
		},
		success: function(res) {
			// console.log(res);
			pullData();
		}
	})
})



$('.query_btn').click(function() {
	query_task_name = $('.query_item_cont').val();
	// console.log($('.query_item_cont').val());
	pullData();
})
$('.subject_sub').on('click', '.dian', function() {
	let html = $(this).parent().parent().find('.event_desc').text();
	// console.log(html);
	$('.cont').html(html);
	$('.detailedTask').css('display', 'block');
})
$('.yiyue').click(function() {
	$('.detailedTask').css('display', 'none');
})

$('.subject_sub').on('click', '.edit', function() {
	$('.add_content2').css('display', 'block');
	let task_id = $(this).parent().parent().find('.event_id').text();
	// console.log(task_id);
	let task_name = $(this).parent().parent().find('.event_name').text();
	let task_desc = $(this).parent().parent().find('.event_desc').text();
	let task_state = $(this).parent().parent().find('.event_state').text();

	$('.modify_item_name').val(task_name);
	$('.modify_item_desc').val(task_desc);
	$('.modify_item_state').val(task_state);

	$('.modify_complete').click(function() {
		task_name = $('.modify_item_name').val();
		task_desc = $('.modify_item_desc').val();
		task_state = $('.modify_item_state').val();
		$.ajax({
			url: 'http://chonghekj.com/todo/index.php/Home/Task/edit',
			type: 'post',
			dataType: 'json',
			data: {
				user_id: obj.user_id,
				access_token: obj.access_token,
				task_id,
				task_name,
				task_desc,
				task_state
			},
			success: function(res) {
				console.log(res);
				pullData();
				$('.add_content2').css('display', 'none');
			}
		})
	})
})

$('.modify_cancel').click(function() {
	$('.add_content2').css('display', 'none');
})

$('.signOut').click(function() {
	$.ajax({
		url: 'http://chonghekj.com/todo/index.php/Home/User/logout',
		type: 'post',
		dataType: 'json',
		data: {
			user_id: obj.user_id,
			access_token: obj.access_token
		},
		success: function(res) {
			console.log(res)
			location.href = `index.html`;
		}
	})
})
